// 引入接口文件
import { submit,getProjectList,searchLeadUserByProject,searchUserByProject,verifyCodeFun} from './teamMannageAddServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  modelObj: {
    btnLoading: false,
  },
  loading: false,
  actionType: 'add',
  areaList: [],
  products: [],
  teamLeaderList:[],
  userList: [],
  query: {
    // areaId: '',
    orgGuid: '', //项目id
    teamName: '', //团队名称
    teamType: '', //团队类型
    teamLeader: '', //管理员
    userList: [], //成员
  }
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // 执行业务操作
          dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态

          dispatch({type: 'getProjectList',payload: {}}) // end dispatch
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
  //获取区域项目
  *getProjectList({payload},{put,select,call}){
    let {areaList} = yield select(state => state[tmpModule.namespace])
    let params = {
        level : 2,
      }
      if(yield call(verifyCodeFun,{})){ 
        const { data } = yield call(getProjectList, {...params})
        let list = data
        if(!list.length){
          return
        }
        areaList =  list.map((item,index)=>{
          return {
            label: item.buname,
            value: item.buguid,
            children: item.childSysUninessunitList
          }
        })
        yield put({type: 'updateStore', payload: {areaList}})
      }
    },
    //搜索团队管理人
    *searchLeadUserByProject({payload},{put,select,call}){
      let {teamLeaderList} = yield select(state => state[tmpModule.namespace])
      let params = {
          orgId : payload.orgGuid
        }
        const { success,data } = yield call(searchLeadUserByProject, {...params})
        if(success){
          // let list = JSON.parse(data)
          let list = data
          if(!list.leadUsers.length){
            return
          }
          teamLeaderList =  list.leadUsers.map((item,index)=>{
            return {
              label: item.name,
              value: item.id,
            }
          })
          yield put({type: 'updateStore', payload: {teamLeaderList}})
        }
      },
      //搜索团队成员
    *searchUserByProject({payload},{put,select,call}){
      let {userList} = yield select(state => state[tmpModule.namespace])
      let params = {
          orgId : payload.orgGuid
        }
        const { success,data } = yield call(searchUserByProject, {...params})
        if(success){
          // let list = JSON.parse(data)
          let list = data
          if(!list.userList.length){
            return
          }
          userList =  list.userList.map((item,index)=>{
            return {
              label: item.name,
              value: item.id,
            }
          })
          yield put({type: 'updateStore', payload: {userList}})
        }
      },
      *submit({payload},{put,select,call}){
        let {query} = yield select(state => state[tmpModule.namespace])
        let params = query
        const { success, msg, data } = yield call(submit, {...params})
        if(success){
          message.success('新增成功')
          hashHistory.push({
            pathname: '/home/expandManage/teamMannage',
            query: {}
          })
        }
      }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
